package com.duyutian.zhanglong.p7客户端和服务端连接;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.UUID;

/**
 * 自定义处理器
 */
public class TestHttpServerHandler2 extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) {
        System.out.println(ctx.channel().remoteAddress() + " msg: " + msg);
        //回写给客户端的信息
        ctx.channel().writeAndFlush("from server :" + UUID.randomUUID());
    }

    /**
     * 发生错误 打印日志
     *
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
